#ifndef ODN_TEXTUREHANDLER_H
#define ODN_TEXTUREHANDLER_H

#include "odn_gfx_Renderer.h"

//Standard library headers
#include <vector>

#include <iostream>

namespace ouden
{
	namespace gfx
	{
		
		class ODN_TextureHandler
		{
			
		private:
			
			struct Texture {
				//Constructor
				Texture(char* Filename, GLuint TexIndex) : filename(Filename), texindex(TexIndex)		{}
				
				//Variables
				char* filename;
				GLuint texindex;
			};
			
		public:
			
			ODN_TextureHandler()		{}
			~ODN_TextureHandler()		{ killAllTextures(); }
			
			//Singleton instance function
			/*static ODN_TextureHandler* Instance()
			{
				static ODN_TextureHandler t;
				return &t;
			}*/
			
			//TODO: Constructor and destructor 
			
			//Main texture function. Will return texture index if already in use or load it from file if it's not 
			GLuint getTexture(char* Filename)
			{
				
				//Loop through textures
				for (int i=0;i<textures.size();i++) {
					
					//Check for texture with this filename 
					if (textures[i].filename == Filename) {
						
						//Return this texture as it is the same as the one we want 
						return textures[i].texindex;
						
					}
					
				}
				
				//Otherwise create a new texture
				return createTexture(Filename); 
				
			}
			
			//Delete all of the textures 
			void killAllTextures()
			{
				for (int i=0;i<textures.size();i++) {
					glDeleteTextures(1, &textures[i].texindex);
				}
				textures.clear(); 
			}
			
		private:
			
			std::vector<Texture> textures;
			
			//Create and load a texture from a file 
			GLuint createTexture(char* Filename);
			
			//Image loading functions 
			GLuint loadTGATexture(char* Filename);
			int CopyByteArray(unsigned char* &DestArray, unsigned char* SourceArray, unsigned int DestStart, unsigned int SourceStart, unsigned int Length);
			unsigned short GetNextPowOf2(unsigned short Number);
			
		};
		
		//Return the singleton instance of the texture handler
		//ODN_TextureHandler* getTextureHandler();
		
	} //namespace gfx
} //namespace ouden

#endif //ODN_TEXTUREHANDLER_H 
